package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.entity.BaseCityVO;
import com.kaili.basic.pojo.entity.BasicSite;
import com.kaili.basic.mapper.BasicSiteMapper;
import com.kaili.basic.service.BasicSiteService;
import com.kaili.basic.service.OpenAccountService;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.PinYinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangjl
 * @Type BasicSiteServerImpl
 * @Desc 杭州凯立通信有限公司
 * @Date 2018年04月26日
 */
@Service(value = "basicSiteService")
public class BasicSiteServiceImpl implements BasicSiteService {

    @Autowired
    private BasicSiteMapper basicSiteMapper;

    @Autowired
    private OpenAccountService openAccountService;

    @Transactional
    @Override
    public boolean addBasicSite(BasicSite basicSite) {
        basicSite.setSitePingyin(PinYinUtil.toSimplePinYin(basicSite.getSiteName()));
        basicSite.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        basicSite.setCreateTime(new Date());
        int flag = basicSiteMapper.insertBasicSite(basicSite);
        if (flag >0){
            Map<String, Object> map=new HashMap<>();
            map.put("accountTypeId",1);
            map.put("dataId",basicSite.getSiteId());
            openAccountService.addAccount(map);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int updateBasicSite(BasicSite basicSite) {
        basicSite.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        basicSite.setModifyTime(new Date());
        basicSite.setSitePingyin(PinYinUtil.toSimplePinYin(basicSite.getSiteName()));
        return basicSiteMapper.updateBasicSite(basicSite);
    }

    @Override
    public void deleteCustomer(Long siteId) {
        Map<String, Object> map = new HashMap();
        Integer uid = LoginDetailUtils.getCurrentEmployeeId();
        if (uid != null) {
            map.put("modifiedBy", uid.longValue());
            map.put("siteId",siteId);
            basicSiteMapper.deleteBasicSite(map);
        }
    }

    @Override
    public BasicSite getBySiteId(Long siteId) {
        return basicSiteMapper.getBySiteId(siteId);
    }


    @Override
    public List<BasicSite> querySiteNameCode(String str) {
        return basicSiteMapper.querySiteNameCode(str);
    }

    @Override
    public Page<BasicSite> listBasicSite(Page<BasicSite> page, BasicSite basicSite){
        List<BasicSite> basicSites = basicSiteMapper.listBasicSite(page,basicSite);
        return page.setRecords(basicSites);
    }

    @Override
    public List<BaseCityVO> getCountyName(BaseCityVO baseCityVO) {
        return basicSiteMapper.getCountyName(baseCityVO);
    }

    @Override
    public List<BasicSite> exportBasicSiteQueryList(Map map){
        return basicSiteMapper.exportBasicSiteQueryList(map);
    }

    @Override
    public BasicSite querySiteName(String name) {
        return basicSiteMapper.querySiteName(name);
    }

    @Override
    public BasicSite getSiteAddressAndCity(Integer siteId) {
        return basicSiteMapper.getSiteAddressAndCity(siteId);
    }
}
